<?php

namespace Kateba\TTICOBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;

class TTICOController extends Controller {

    /**
     * the ttico main description page
     * @author Mahmoud
     */
    public function showAction() {
        $page = $this->getDoctrine()->getEntityManager()->getRepository('KatebaSiteBundle:Page')->find(5);
        if (!$page) {
            throw $this->createNotFoundException('Can not find the requested page');
        }
        return $this->render('KatebaTTICOBundle:TTICO:show.html.twig', array('page' => $page));
    }

    /**
     * the industry training page action
     */
    public function industryTrainingAction() {
        $page = $this->getDoctrine()->getEntityManager()->getRepository('KatebaSiteBundle:Page')->find(11);
        if (!$page) {
            throw $this->createNotFoundException('Can not find the requested page');
        }
        return $this->render('KatebaTTICOBundle:TTICO:industry_training.html.twig', array('page' => $page));
    }
    
    public function submitYourInventionAction() {
        $page = $this->getDoctrine()->getEntityManager()->getRepository('KatebaSiteBundle:Page')->find(9);
        if (!$page) {
            throw $this->createNotFoundException('Can not find the requested page');
        }
        return $this->render('KatebaTTICOBundle:TTICO:submit_your_invention.html.twig', array('page' => $page));
    }
    
    public function protectYourInventionAction() {
        $page = $this->getDoctrine()->getEntityManager()->getRepository('KatebaSiteBundle:Page')->find(10);
        if (!$page) {
            throw $this->createNotFoundException('Can not find the requested page');
        }
        return $this->render('KatebaTTICOBundle:TTICO:protect_your_invention.html.twig', array('page' => $page));
    }

    /**
     * get $count latest calls from the data base
     * @param integer $count the number of calls to retrieve
     */
    public function getlatestNewsAction($count = 5) {
        $entities = $this->getDoctrine()->getEntityManager()->getRepository('KatebaTTICOBundle:Post')->getLatestNews($count);
        return $this->render('KatebaTTICOBundle:TTICO:latestNews.html.twig', array(
                    'entities' => $entities
                ));
    }

    /**
     * get $count latest courses from the data base
     * @param integer $count the number of calls to retrieve
     */
    public function getlatestCoursesAction($count = 5) {
        $entities = $this->getDoctrine()->getEntityManager()->getRepository('KatebaTTICOBundle:Post')->getLatestCourses($count);
        return $this->render('KatebaTTICOBundle:TTICO:latestCourses.html.twig', array(
                    'entities' => $entities
                ));
    }

    /**
     * get $count latest technologies from the data base
     * @param integer $count the number of calls to retrieve
     */
    public function getlatestTechnologiesAction($count = 5) {
        $entities = $this->getDoctrine()->getEntityManager()->getRepository('KatebaTTICOBundle:Technology')->getLatestTechnologies($count);
        return $this->render('KatebaTTICOBundle:TTICO:latestTechnologies.html.twig', array(
                    'entities' => $entities
                ));
    }

    public function showPostTypeAction($type, $page) {
        //the results per page number
        $itemsPerPage = 6;
        $postRepo = $em = $this->getDoctrine()->getEntityManager()->getRepository('KatebaTTICOBundle:Post');
        $results = $postRepo->getPosts($type, $page, $itemsPerPage);
        $entities = $results['entities'];
        //the number of posts
        $count = $results['count'];
        //calculate the last page number
        $lastPageNumber = (int) ($count / $itemsPerPage);
        if (($count % $itemsPerPage) > 0) {
            $lastPageNumber++;
        }
        return $this->render('KatebaTTICOBundle:TTICO:post_type_results.html.twig', array(
                    'entities' => $entities,
                    'count' => $count,
                    'page' => $page,
                    'lastPageNumber' => $lastPageNumber,
                    'type' => $type
                ));
    }

    public function technologiesAction($page) {
        //the results per page number
        $itemsPerPage = 9;
        $results = $this->getDoctrine()->getEntityManager()->getRepository('KatebaTTICOBundle:Technology')->getTechnologies($page, $itemsPerPage);
        $entities = $results['entities'];
        //the number of posts
        $count = $results['count'];
        //calculate the last page number
        $lastPageNumber = (int) ($count / $itemsPerPage);
        if (($count % $itemsPerPage) > 0) {
            $lastPageNumber++;
        }
        return $this->render('KatebaTTICOBundle:TTICO:technologies.html.twig', array(
                    'entities' => $entities,
                    'count' => $count,
                    'page' => $page,
                    'lastPageNumber' => $lastPageNumber
                ));
    }

    public function showPostAction($id) {
        $post = $this->getDoctrine()->getEntityManager()->getRepository('KatebaTTICOBundle:Post')->find($id);
        if (!$post) {
            throw $this->createNotFoundException('Can not find the requested post');
        }
        return $this->render('KatebaTTICOBundle:TTICO:post.html.twig', array('post' => $post));
    }

    public function showPartnerAction($id) {
        $partner = $this->getDoctrine()->getEntityManager()->getRepository('KatebaTTICOBundle:ResearchPartner')->find($id);
        if (!$partner) {
            throw $this->createNotFoundException('Can not find the requested partner');
        }
        return $this->render('KatebaTTICOBundle:TTICO:partner.html.twig', array('partner' => $partner));
    }

    public function showTechnologyAction($id) {
        $technology = $this->getDoctrine()->getEntityManager()->getRepository('KatebaTTICOBundle:Technology')->find($id);
        if (!$technology) {
            throw $this->createNotFoundException('Can not find the requested technology');
        }
        return $this->render('KatebaTTICOBundle:TTICO:technology.html.twig', array('technology' => $technology));
    }

    public function searchForPartnerAction() {
        $form = $this->createFormBuilder(null, array('csrf_protection' => false))
                ->add('theme', 'entity', array(
                    'required' => false,
                    'class' => 'KatebaTTICOBundle:Theme',
                    'expanded' => false,
                    'multiple' => true,
                    'attr' => array('style' => 'width: 100%;', 'data-placeholder' => "Choose a theme...")
                ))
                ->add('type', 'hidden', array('attr' => array('value' => 0)))
                ->getForm();
        return $this->render('KatebaTTICOBundle:TTICO:search.html.twig', array('form' => $form->createView(), 'searchType' => 0));
    }

    public function searchForTechnologyAction() {
        $form = $this->createFormBuilder(null, array('csrf_protection' => false))
                ->add('theme', 'entity', array(
                    'required' => false,
                    'class' => 'KatebaTTICOBundle:Theme',
                    'expanded' => false,
                    'multiple' => true,
                    'attr' => array('style' => 'width: 100%;', 'data-placeholder' => "Choose a theme...")
                ))
                ->add('type', 'hidden', array('attr' => array('value' => 1)))
                ->getForm();
        return $this->render('KatebaTTICOBundle:TTICO:search.html.twig', array('form' => $form->createView(), 'searchType' => 1));
    }

    public function searchResultsAction($page) {
        $form = $this->getRequest()->get('form', FALSE);
        $type = $form['type'];
        if (isset($form['theme'])) {
            $themes = $form['theme'];
        } else {
            $themes = array();
        }
        $itemsPerPage = 6;
        if ($type == 0) {
            $results = $this->getDoctrine()->getEntityManager()->getRepository('KatebaTTICOBundle:ResearchPartner')->getResults($themes, $page, $itemsPerPage);
        } else {
            $results = $this->getDoctrine()->getEntityManager()->getRepository('KatebaTTICOBundle:Technology')->getResults($themes, $page, $itemsPerPage);
        }
        $entities = $results['entities'];
        //the number of posts
        $count = $results['count'];
        //calculate the last page number
        $lastPageNumber = (int) ($count / $itemsPerPage);
        if (($count % $itemsPerPage) > 0) {
            $lastPageNumber++;
        }
        foreach ($entities as &$entity){
            $entity->setContent(substr($entity->getContent(), 0, 255));
        }
        return $this->render('KatebaTTICOBundle:TTICO:searchResults.html.twig', array(
                    'entities' => $entities,
                    'count' => $count,
                    'page' => $page,
                    'lastPageNumber' => $lastPageNumber,
                    'type' => $type
                ));
    }

}
